package com.castle.fortress.admin.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.goods.entity.GoodsOptionEntity;
import com.castle.fortress.admin.goods.dto.GoodsOptionDto;
import com.castle.fortress.admin.goods.mapper.GoodsOptionMapper;
import com.castle.fortress.admin.goods.service.GoodsOptionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 商品规格项目详情 服务实现类
 *
 * @author majunjie
 * @since 2021-11-25
 */
@Service
public class GoodsOptionServiceImpl extends ServiceImpl<GoodsOptionMapper, GoodsOptionEntity> implements GoodsOptionService {
    /**
    * 获取查询条件
    * @param goodsOptionDto
    * @return
    */
    public QueryWrapper<GoodsOptionEntity> getWrapper(GoodsOptionDto goodsOptionDto){
        QueryWrapper<GoodsOptionEntity> wrapper= new QueryWrapper();
        if(goodsOptionDto != null){
            GoodsOptionEntity goodsOptionEntity = ConvertUtil.transformObj(goodsOptionDto,GoodsOptionEntity.class);
            wrapper.like(goodsOptionEntity.getId() != null,"id",goodsOptionEntity.getId());
            wrapper.like(goodsOptionEntity.getGoodsId() != null,"goods_id",goodsOptionEntity.getGoodsId());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getSpecs()),"specs",goodsOptionEntity.getSpecs());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getName()),"name",goodsOptionEntity.getName());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getProductPrice()),"product_price",goodsOptionEntity.getProductPrice());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getMarketPrice()),"market_price",goodsOptionEntity.getMarketPrice());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getCostPrice()),"cost_price",goodsOptionEntity.getCostPrice());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getStock()),"stock",goodsOptionEntity.getStock());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getWeight()),"weight",goodsOptionEntity.getWeight());
            wrapper.like(goodsOptionEntity.getSort() != null,"sort",goodsOptionEntity.getSort());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getGoodsSn()),"goods_sn",goodsOptionEntity.getGoodsSn());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getProductSn()),"product_sn",goodsOptionEntity.getProductSn());
            wrapper.like(StrUtil.isNotEmpty(goodsOptionEntity.getRemark()),"remark",goodsOptionEntity.getRemark());
            wrapper.like(goodsOptionEntity.getCreateUser() != null,"create_user",goodsOptionEntity.getCreateUser());
            wrapper.like(goodsOptionEntity.getCreateTime() != null,"create_time",goodsOptionEntity.getCreateTime());
            wrapper.like(goodsOptionEntity.getUpdateUser() != null,"update_user",goodsOptionEntity.getUpdateUser());
            wrapper.like(goodsOptionEntity.getUpdateTime() != null,"update_time",goodsOptionEntity.getUpdateTime());
            wrapper.like(goodsOptionEntity.getStatus() != null,"status",goodsOptionEntity.getStatus());
            wrapper.like(goodsOptionEntity.getIsDeleted() != null,"is_deleted",goodsOptionEntity.getIsDeleted());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<GoodsOptionDto> pageGoodsOption(Page<GoodsOptionDto> page, GoodsOptionDto goodsOptionDto) {
        QueryWrapper<GoodsOptionEntity> wrapper = getWrapper(goodsOptionDto);
        Page<GoodsOptionEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<GoodsOptionEntity> goodsOptionPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<GoodsOptionDto> pageDto = new Page(goodsOptionPage.getCurrent(), goodsOptionPage.getSize(),goodsOptionPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(goodsOptionPage.getRecords(),GoodsOptionDto.class));
        return pageDto;
    }

    @Override
    public IPage<GoodsOptionDto> pageGoodsOptionExtends(Page<GoodsOptionDto> page, GoodsOptionDto goodsOptionDto){
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        GoodsOptionEntity goodsOptionEntity = ConvertUtil.transformObj(goodsOptionDto,GoodsOptionEntity.class);
        List<GoodsOptionEntity> goodsOptionList=baseMapper.extendsList(pageMap,goodsOptionEntity);
        Long total = baseMapper.extendsCount(goodsOptionEntity);
        Page<GoodsOptionDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(goodsOptionList,GoodsOptionDto.class));
        return pageDto;
    }
    @Override
    public GoodsOptionDto getByIdExtends(Long id){
        GoodsOptionEntity  goodsOptionEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(goodsOptionEntity,GoodsOptionDto.class);
    }

    @Override
    public List<GoodsOptionDto> listGoodsOption(GoodsOptionDto goodsOptionDto){
        QueryWrapper<GoodsOptionEntity> wrapper = getWrapper(goodsOptionDto);
        List<GoodsOptionEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,GoodsOptionDto.class);
    }

    @Override
    public void deleteByGoodsId(Long goodsId) {
        if(goodsId == null){
            return;
        }
        baseMapper.deleteByGoodsId(goodsId);
    }

    @Override
    public List<GoodsOptionDto> listByGoodsId(Long goodsId) {
        if(goodsId == null){
            return new ArrayList<>();
        }
        Map<String , Object> params = new HashMap<>();
        params.put("goodsId" , goodsId);
        List<GoodsOptionDto> list =ConvertUtil.transformObjList(baseMapper.selectDataList(params) , GoodsOptionDto.class);
        return list;
    }
}

